package com.example.flower_shop.controller.admin;

import com.example.flower_shop.common.ApiResponse;
import com.example.flower_shop.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/upload")
@PreAuthorize("hasRole('MERCHANT')")
public class UploadController {

    @Autowired
    private FileService fileService;

    @PostMapping("/image")
    public ApiResponse<Map<String, String>> uploadImage(@RequestParam("file") MultipartFile file) {
        log.info("接收到图片上传请求");
        try {
            String url = fileService.uploadImage(file);
            log.info("图片上传成功，URL: {}", url);

            Map<String, String> data = new HashMap<>();
            data.put("url", url);
            return ApiResponse.success("图片上传成功", data);
        } catch (Exception e) {
            log.error("图片上传失败", e);
            return ApiResponse.error("图片上传失败：" + e.getMessage());
        }
    }
}